package com.learning.jquizenglish.network;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.learning.jquizenglish.JQuizAppConfig;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.screen.profile.ProfilePreference;

// TODO: Auto-generated Javadoc
/**
 * The Class JQuizEnglishApi.
 */
public class JQuizEnglishApi {

	/** The Constant BASE_URL. */
	public static final String BASE_URL = "http://flappybridmmo.com/quiz/home/";
	
	public static final String BASE_AUDIO_URL = "http://flappybridmmo.com/quiz/medias/audio/";
	
	public static final String BASE_QUITLET_URL = "https://api.quizlet.com/2.0/";
	
	/** The Constant REGISTER_API. */
	public static final String REGISTER_API = BASE_URL+"reg_user";
	
	/** The Constant LEARNING_FEED_API. */
	public static final String LEARNING_FEED_API = BASE_URL+"questions_feed";
	
	/** The Constant FLASHCARD_API. */
	public static final String FLASHCARD_API = BASE_URL+"categories";
	
	/** The Constant SAVE_SCORE_API. */
	public static final String SAVE_SCORE_API = BASE_URL+"history";
	
	/** The Constant LIST_HIGH_SCORE_API. */
	public static final String LIST_HIGH_SCORE_API = BASE_URL+"history";
	
	/** The Constant COMMENT_API. */
	public static final String COMMENT_API = BASE_URL+"comment";
	
	/** The Constant HISTORY_API. */
	public static final String HISTORY_API = BASE_URL+"user_score";
	
	public static final String BASE_IMAGE_URL = "http://flappybridmmo.com/quiz/medias/img/users/";
	
	public static final String CLIENT_QUIZLET_ID = "CjCWEAySgW";
	
	public static final String SECRET_QUIZLET_KEY = "eUgz2SBJKetMBCWxm9sFdd";
	/**
	 * Gets the request question.
	 *
	 * @param questionType the question type
	 * @param level the level
	 * @param limit the limit
	 * @return the request question
	 */
	public static JQuizRequestModel getRequestQuestion(QuestionType questionType,Level level,int limit){
		int appType = AppType.GRAMMAR.getValue();
		if(JQuizAppConfig.IS_LISTENING_APP){
			appType = AppType.LISTENING.getValue();
		}
		String url = BASE_URL+"questions?types_id="
				+ questionType.getValue()+"&type_quiz="
				+ appType+"&level_id="
				+ level.getValue()+"&limit="
				+ limit;
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = url;
		return requestModel;
	}
	
	public static JQuizRequestModel getRequestQuestionOne(String questionId){
		String url = BASE_URL+"questions_one?id=" +questionId;
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = url;
		return requestModel;
	}
	
	/**
	 * Gets the request profile.
	 *
	 * @param context the context
	 * @return the request profile
	 */
	public static JQuizRequestModel getRequestProfile(Context context){
		TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceIMEI = tManager.getDeviceId();
		AppType appType = AppType.GRAMMAR;
		if (JQuizAppConfig.IS_LISTENING_APP) {
			appType = AppType.LISTENING;
		}
		//setup params
		List<NameValuePair> mPairs = new ArrayList<NameValuePair>();
		mPairs.add(new BasicNameValuePair("action", "register"));
		mPairs.add(new BasicNameValuePair("imei", deviceIMEI));
		mPairs.add(new BasicNameValuePair("type_quiz", String.valueOf(appType.getValue())));
		//create Request Model
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = REGISTER_API;
		requestModel.listNameValuePairs = mPairs;
		return requestModel;
	}
	
	/**
	 * Gets the request learning feed.
	 *
	 * @return the request learning feed
	 */
	public static JQuizRequestModel getRequestLearningFeed(){
		int appType = AppType.GRAMMAR.getValue();
		if(JQuizAppConfig.IS_LISTENING_APP){
			appType = AppType.LISTENING.getValue();
		}
		//create Request Model
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = LEARNING_FEED_API+"?type_quiz="+appType;
		return requestModel;
	}
	
	/**
	 * Gets the request flash card categories.
	 *
	 * @param level the level
	 * @return the request flash card categories
	 */
	public static JQuizRequestModel getRequestFlashCardCategories(Level level){
		//create Request Model
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = FLASHCARD_API+"?level_id="+level.getValue();
		return requestModel;
	}
	
	/**
	 * Gets the request flash card question.
	 *
	 * @param questionType the question type
	 * @param level the level
	 * @param cateId the cate id
	 * @return the request flash card question
	 */
	public static JQuizRequestModel getRequestFlashCardQuestion(QuestionType questionType,Level level,String cateId){
		int appType = AppType.GRAMMAR.getValue();
		if(JQuizAppConfig.IS_LISTENING_APP){
			appType = AppType.LISTENING.getValue();
		}
		String url = BASE_URL+"questions?types_id="+ questionType.getValue()
				+"&type_quiz="+ appType
				+"&level_id="+ level.getValue()
				+"&categories_id="+cateId;
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = url;
		return requestModel;
	}
	
	/**
	 * Gets the request save score.
	 *
	 * @param context the context
	 * @param levelId the level id
	 * @param score the score
	 * @param percent the percent
	 * @param jsonAnswer the json answer
	 * @param type the type
	 * @return the request save score
	 */
	public static JQuizRequestModel getRequestSaveScore(Context context,Level levelId,int score,String percent,String jsonAnswer,AppType type){
		TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceIMEI = tManager.getDeviceId();
		//setup params
		List<NameValuePair> mPairs = new ArrayList<NameValuePair>();
		mPairs.add(new BasicNameValuePair("imei", deviceIMEI));
		mPairs.add(new BasicNameValuePair("level_id", String.valueOf(levelId.getValue())));
		mPairs.add(new BasicNameValuePair("score", String.valueOf(score)));
		mPairs.add(new BasicNameValuePair("percent", percent));
		mPairs.add(new BasicNameValuePair("rs_list_question", jsonAnswer));
		mPairs.add(new BasicNameValuePair("type_quiz", String.valueOf(type.getValue())));
		//create Request Model
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = SAVE_SCORE_API;
		requestModel.listNameValuePairs = mPairs;
		return requestModel;
	}
	
	/**
	 * Gets the request list high score.
	 *
	 * @return the request list high score
	 */
	public static JQuizRequestModel getRequestListHighScore(){
		AppType appType = AppType.GRAMMAR;
		if (JQuizAppConfig.IS_LISTENING_APP) {
			appType = AppType.LISTENING;
		}
		String requestUrl = LIST_HIGH_SCORE_API+"?type_quiz="+appType.getValue();
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = requestUrl;
		return requestModel;
	}
	
	/**
	 * Gets the request post comment.
	 *
	 * @param questionId the question id
	 * @param content the content
	 * @return the request post comment
	 */
	public static JQuizRequestModel getRequestPostComment(Context context,String questionId,String content){
		TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceIMEI = tManager.getDeviceId();
		//setup params
		List<NameValuePair> mPairs = new ArrayList<NameValuePair>();
		mPairs.add(new BasicNameValuePair("question_id", questionId));
		mPairs.add(new BasicNameValuePair("content", content));
		mPairs.add(new BasicNameValuePair("imei", deviceIMEI));
		//create Request Model
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = COMMENT_API;
		requestModel.listNameValuePairs = mPairs;
		return requestModel;
	}
	
	/**
	 * Gets the request list history.
	 *
	 * @param context the context
	 * @return the request list history
	 */
	public static JQuizRequestModel getRequestListHistory(Context context){
		AppType appType = AppType.GRAMMAR;
		if (JQuizAppConfig.IS_LISTENING_APP) {
			appType = AppType.LISTENING;
		}
		TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceIMEI = tManager.getDeviceId();
		String url = HISTORY_API+"?imei="+ deviceIMEI+"&type_quiz="+appType.getValue();
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = url;
		return requestModel;
	}
	
	public static JQuizRequestModel getRequestUpdateProfile(String jsonData){
		//setup params
		List<NameValuePair> mPairs = new ArrayList<NameValuePair>();
		mPairs.add(new BasicNameValuePair("action", "update"));
		mPairs.add(new BasicNameValuePair("data", jsonData));
		//create Request Model
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = REGISTER_API;
		requestModel.listNameValuePairs = mPairs;
		return requestModel;
	}
	
	public static JQuizRequestModel getRequestChangeAvatar(Context context,String imgPath){
		TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceIMEI = tManager.getDeviceId();
		
		MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
		try {
			reqEntity.addPart("action", new StringBody("change_picture"));
			reqEntity.addPart("imei", new StringBody(deviceIMEI));
			File file = new File(imgPath);
            FileBody image = new FileBody(file);
            reqEntity.addPart("avatar", image);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = REGISTER_API;
		requestModel.multipartEntity = reqEntity;
		return requestModel;
	}
	
	public static JQuizRequestModel getRequestFlashCardCategory(Level levelId,int pageNumber,int pageCount){
		String searchKey = "Elementary";
		switch (levelId) {
		case ELEMENTARY:
			searchKey = "Elementary";
			break;
		case INTERMEDIATE:
			searchKey = "Intermediate";
			break;
		case ADVANCED:
			searchKey = "Advance";
			break;
		default:
			break;
		}
		String url = BASE_QUITLET_URL+"search/sets?client_id="+CLIENT_QUIZLET_ID
				+"&q=" + searchKey
				+"&page=" +pageNumber
				+"&per_page="+pageCount;
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = url;
		return requestModel;
	}
	
	public static JQuizRequestModel getRequestFlashCardDetail(long cateId){
		String url = BASE_QUITLET_URL+"sets/"+cateId+"?client_id="+CLIENT_QUIZLET_ID;
		JQuizRequestModel requestModel = new JQuizRequestModel();
		requestModel.url = url;
		return requestModel;
	}
	
	/**
	 * The Enum QuestionType.
	 */
	public enum QuestionType{
		
		/** The question. */
		QUESTION(1),
		
		/** The flashcard. */
		FLASHCARD(2);
		
		/** The value. */
		private final int value;

	    /**
    	 * Instantiates a new question type.
    	 *
    	 * @param value the value
    	 */
    	private QuestionType(int value) {
	        this.value = value;
	    }

	    /**
    	 * Gets the value.
    	 *
    	 * @return the value
    	 */
    	public int getValue() {
	        return value;
	    }
	}
	
	/**
	 * The Enum AppType.
	 */
	public enum AppType{
		
		/** The grammar. */
		GRAMMAR(0),
		
		/** The listening. */
		LISTENING(1);
		
		/** The value. */
		private final int value;

	    /**
    	 * Instantiates a new app type.
    	 *
    	 * @param value the value
    	 */
    	private AppType(int value) {
	        this.value = value;
	    }

	    /**
    	 * Gets the value.
    	 *
    	 * @return the value
    	 */
    	public int getValue() {
	        return value;
	    }
	}
	
	/**
	 * The Enum Level.
	 */
	public enum Level{
		
		/** The elementary. */
		ELEMENTARY(1),
		
		/** The intermediate. */
		INTERMEDIATE(2),
		
		/** The advanced. */
		ADVANCED(3);
		
		/** The value. */
		private final int value;

	    /**
    	 * Instantiates a new level.
    	 *
    	 * @param value the value
    	 */
    	private Level(int value) {
	        this.value = value;
	    }

	    /**
    	 * Gets the value.
    	 *
    	 * @return the value
    	 */
    	public int getValue() {
	        return value;
	    }
	}
}
